package leetcode.easy;

/**
 * ClassName: $605_PlaceFlower
 * Description:
 * date: 2021-10-16 21:43
 *
 * @author liyifan
 */
public class $605_PlaceFlower {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        if (n == 0)
            return true;
        if (flowerbed.length == 1) {
            return flowerbed[0] == 0;
        }
        for (int i = 0;i < flowerbed.length;i++) {
            if (n != 0) {
                if (flowerbed[i] == 0) {
                    if (i == 0) {
                        if (flowerbed[i + 1] == 0) {
                            // 第一个花坛可以种
                            n--;
                            flowerbed[i] = 1;
                        }
                    } else if (i == flowerbed.length - 1) {
                        if (flowerbed[i - 1] == 0) {
                            n--;
                            flowerbed[i] = 1;
                        }
                    } else if (flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0) {
                        n--;
                        flowerbed[i] = 1;
                    }
                } else {
                    continue;
                }
            } else {
                break;
            }
        }
        return n == 0;
    }

    public boolean canPlaceFlowers2(int[] flowerbed, int n){
        for (int i = 0;i < flowerbed.length && n > 0;) {
            if (flowerbed[i] == 1) {
                i += 2;
            } else if (flowerbed[i + 1] == 0 || i == flowerbed.length - 1) {
                n--;
                i += 2;
            } else {
                i += 3;
            }
        }
        return n <= 0;
    }

}
